package com.belle.sfsdfasdfsdf.controller;

import com.belle.sfsdfasdfsdf.domain.ScheduleJob;
import com.belle.sfsdfasdfsdf.response.ReturnObject;
import com.belle.sfsdfasdfsdf.service.JobService;
import com.belle.sfsdfasdfsdf.support.QuartzJobFactory;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("quartz")
public class QuartzController {
	
	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;
	
	@Autowired
	private JobService jobService;
	
	/**
	 * 添加一个定时任务
	 * @author sxl
	 * @date 2018/9/18 0018 14:58
	 * @param job
	 * @return void
	 */
	@PostMapping("/addOrUpdate")
	@ResponseBody
	public ReturnObject addOrUpdateTask(@RequestBody ScheduleJob job){
		if(StringUtils.isEmpty(job.getJobName()) ||
				StringUtils.isEmpty(job.getJobGroup()) ||
				StringUtils.isEmpty(job.getCronExpression())) {
			return ReturnObject.getParamErrorReturn();
		}
		try {
			Scheduler scheduler = schedulerFactoryBean.getScheduler();
			//获取触发器标识
			TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
			//获取触发器trigger
			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
			if(null==trigger){
				//创建任务
				JobDetail jobDetail = JobBuilder.newJob(QuartzJobFactory.class)
						.withIdentity(job.getJobName(), job.getJobGroup())
						.build();
				jobDetail.getJobDataMap().put("scheduleJob", job);
				
				//表达式调度构建器
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
						.getCronExpression());
				
				//按新的cronExpression表达式构建一个新的trigger
				trigger = TriggerBuilder.newTrigger()
						.withIdentity(job.getJobName(), job.getJobGroup())
						.withSchedule(scheduleBuilder)
						.build();
				
				scheduler.scheduleJob(jobDetail, trigger);
				
				//把任务插入数据库
				jobService.add(job);
			} else {
				// Trigger已存在，那么更新相应的定时设置
				//表达式调度构建器
				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
						.getCronExpression());
				
				//按新的cronExpression表达式重新构建trigger
				trigger = trigger.getTriggerBuilder()
						.withIdentity(triggerKey)
						.withSchedule(scheduleBuilder)
						.build();
				
				//按新的trigger重新设置job执行
				scheduler.rescheduleJob(triggerKey, trigger);
				
				//更新数据库中的任务
				jobService.update(job);
			}
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return ReturnObject.getDefaultSuccessReturn();
	}
	
	@DeleteMapping("/delete")
	@ResponseBody
	public ReturnObject deleteTask(@RequestBody ScheduleJob job) {
		if(StringUtils.isEmpty(job.getJobName()) ||
				StringUtils.isEmpty(job.getJobGroup())) {
			return ReturnObject.getParamErrorReturn();
		}
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
		try {
			if(scheduler != null) {
				Trigger trigger = scheduler.getTrigger(triggerKey);
				if(trigger != null) {
					//从scheduler中移除这个触发器和它上关联的job
					scheduler.unscheduleJob(triggerKey);
				}
			}
		} catch(SchedulerException e) {
		
		}
		//从数据库删除
		jobService.delete(job.getJobName(), job.getJobGroup());
		
		return ReturnObject.getDefaultSuccessReturn();
	}
	
}
